package com.example.pt.service.impl;


import com.example.pt.entity.Torrent;
import com.example.pt.repository.TorrentRepository;
import com.example.pt.service.TorrentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class TorrentServiceImpl implements TorrentService {
    @Autowired
    private TorrentRepository torrentRepository;
    @Override
    public List<Torrent> getRecommendedTorrents() {
//        // 获取最新上传前5个
//        List<Torrent> latest = lambdaQuery()
//                .orderByDesc(Torrent::getUploadedAt)
//                .last("LIMIT 5")
//                .list();

        // 获取下载最多前5个
        List<Torrent> popular = torrentRepository.findTop5ByDownloadCount();

        // 合并去重（简单处理）
        Set<Torrent> result = new LinkedHashSet<>();
//        result.addAll(latest);
        result.addAll(popular);
        return new ArrayList<>(result);
    }

    @Override
    public Optional<Torrent> getDetailById(Long id) {
        return torrentRepository.findByTorrentId(id);
    }
    @Override
    public List<Torrent> getDiscountedTorrents() {
        List<Torrent> list = torrentRepository.findTop5ByLowestDownloadCount();

        for (Torrent t : list) {
            t.setDiscount(0.5f); // 打五折
        }

        return list;
    }

    @Override
    public List<Torrent> searchTorrents(String name) {
        return torrentRepository.findByNameContainingIgnoreCase(name);
    }


}

